package com.example.blog.controller;

import com.example.blog.dto.FavoriteDTO;

import com.example.blog.pojo.Result;
import com.example.blog.service.FavoriteService;
import com.example.blog.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/favorite")
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

//  获取用户收藏列表
    @GetMapping("/list")
    public Result<List<FavoriteDTO>> getfavoritelist(){

        //获取当前用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");
        List<FavoriteDTO> list = favoriteService.getFavoriteList(userid);
        return Result.success(list);

    }

//    用户删除收藏
    @DeleteMapping("/delete")
    public Result deletefavorite(@RequestParam("id")Integer id){

        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");

        if(favoriteService.deleteFavorite(id,userid)){
            return Result.success();
        }
        return Result.error("操作失败!");

    }
//    用户添加收藏
    @PostMapping("/add")
    public Result addfavorite(@RequestParam("id")Integer articleid){

        boolean insert = favoriteService.addFavorite(articleid);
        if(insert){
            return Result.success();
        }
        else{
            return Result.error("收藏失败!");
        }
    }

}
